#!/usr/bin/env python3
"""
初始化默认平台模板脚本
"""
import sys
import os

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.db.session import SessionLocal
from app.models.platform_template import PlatformTemplate


def init_default_templates():
    """初始化默认平台模板"""
    db = SessionLocal()

    try:
        # 检查是否已存在模板
        existing_count = db.query(PlatformTemplate).count()
        if existing_count > 0:
            print(f"已存在 {existing_count} 个平台模板，跳过初始化")
            return

        # 创建默认平台模板
        templates = [
            PlatformTemplate(
                platform_name='douyin_short',
                template_name='douyin_short',
                display_name='抖音短视频',
                description='60秒竖版短视频，适合抖音平台',
                target_duration=60,
                max_segments=8,
                opening_style='dynamic',
                transition_style='cut',
                background_music_style='trending',
                subtitle_style='bold',
                is_active=True,
                is_default=True,
                created_by='system'
            ),
            PlatformTemplate(
                platform_name='xiaohongshu',
                template_name='xiaohongshu',
                display_name='小红书',
                description='45秒生活分享视频，适合小红书平台',
                target_duration=45,
                max_segments=6,
                opening_style='elegant',
                transition_style='fade',
                background_music_style='lifestyle',
                subtitle_style='clean',
                is_active=True,
                is_default=False,
                created_by='system'
            ),
            PlatformTemplate(
                platform_name='bilibili',
                template_name='bilibili',
                display_name='B站',
                description='3分钟内容视频，适合B站平台',
                target_duration=180,
                max_segments=15,
                opening_style='intro',
                transition_style='dissolve',
                background_music_style='background',
                subtitle_style='readable',
                is_active=True,
                is_default=False,
                created_by='system'
            ),
            PlatformTemplate(
                platform_name='weibo_video',
                template_name='weibo_video',
                display_name='微博视频',
                description='30秒快速分享视频，适合微博平台',
                target_duration=30,
                max_segments=5,
                opening_style='quick',
                transition_style='cut',
                background_music_style='viral',
                subtitle_style='highlight',
                is_active=True,
                is_default=False,
                created_by='system'
            )
        ]

        # 添加内容规则和剪辑规则
        content_rules = {
            'douyin_short': {
                'prefer_high_sentiment': True,
                'min_segment_duration': 3,
                'max_segment_duration': 15,
                'prefer_speech': True,
                'avoid_silence': True,
                'require_high_engagement': True
            },
            'xiaohongshu': {
                'prefer_high_sentiment': True,
                'min_segment_duration': 4,
                'max_segment_duration': 12,
                'prefer_speech': True,
                'require_complete_sentences': True,
                'require_visual_appeal': True
            },
            'bilibili': {
                'prefer_high_sentiment': False,
                'min_segment_duration': 5,
                'max_segment_duration': 30,
                'prefer_speech': True,
                'allow_longer_segments': True,
                'preserve_content_flow': True
            },
            'weibo_video': {
                'prefer_high_sentiment': True,
                'min_segment_duration': 2,
                'max_segment_duration': 10,
                'prefer_speech': True,
                'require_high_engagement': True,
                'optimize_for_sharing': True
            }
        }

        editing_rules = {
            'douyin_short': {
                'add_intro': True,
                'add_outro': True,
                'transition_style': 'cut',
                'add_subtitles': True,
                'add_text_overlays': True,
                'aspect_ratio': '9:16'
            },
            'xiaohongshu': {
                'add_intro': True,
                'add_outro': True,
                'transition_style': 'fade',
                'add_subtitles': True,
                'add_background_music': True,
                'aspect_ratio': '4:5'
            },
            'bilibili': {
                'add_intro': True,
                'add_outro': True,
                'transition_style': 'dissolve',
                'add_subtitles': True,
                'preserve_content_flow': True,
                'aspect_ratio': '16:9'
            },
            'weibo_video': {
                'add_intro': True,
                'add_outro': True,
                'transition_style': 'cut',
                'add_subtitles': True,
                'add_text_overlays': True,
                'aspect_ratio': '16:9'
            }
        }

        # 为每个模板添加详细配置
        for template in templates:
            platform_name = template.platform_name

            # 添加内容规则
            if platform_name in content_rules:
                template.content_rules = content_rules[platform_name]

            # 添加剪辑规则
            if platform_name in editing_rules:
                template.editing_rules = editing_rules[platform_name]

            db.add(template)
            print(f"创建模板: {template.display_name}")

        db.commit()
        print(f"成功创建 {len(templates)} 个默认平台模板")

    except Exception as e:
        print(f"初始化平台模板失败: {e}")
        db.rollback()
        raise
    finally:
        db.close()


if __name__ == "__main__":
    init_default_templates()